package xin.marcher.module.rights.runner;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.rights.domain.dto.UserDTO;
import xin.marcher.module.rights.domain.request.QueryUserRequest;
import xin.marcher.module.rights.domain.request.UserRequest;
import xin.marcher.module.rights.service.UserService;

/**
 * 生成默认的管理员账号
 */
@Component
public class AdminUserRunner implements ApplicationRunner {

    @Autowired
    private UserService userService;

    @Override
    public void run(ApplicationArguments args) throws Exception {

        QueryUserRequest request = new QueryUserRequest();
        request.setUsername("admin");
        PageResult<UserDTO> userDTOPageResult = userService.queryUser(request);

        if (CollectionUtils.isEmpty(userDTOPageResult.getContent())) {
            UserRequest req = new UserRequest();
            req.setUsername("admin");
            req.setEmail("admin@xxx.com");
            req.setPassword("123456");
            req.setPhoneNumber("18520222022");
            req.setOperateUser(1);
            userService.saveUser(req);
        }
    }
}
